package com.service.scheduler.job;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.StatefulJob;

import com.service.bean.company.Company;
import com.service.cache.CacheManager;
import com.service.db.DbManager;
import com.service.other.Logger;

/**
 * 将缓存中的Gps数据批量保存到数据库中 
 * @author lj
 */
@SuppressWarnings("deprecation")
public class SaveGpsJob implements StatefulJob {
	
	public SaveGpsJob() {
		
	}
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		try {
			JobKey jk = context.getJobDetail().getKey();
			String jobName = jk.getName();
			String jobGroup = jk.getGroup();
			Logger.println("启动任务 [" + jobGroup + "].[" + jobName + "]");
			for(Company company : CacheManager.instance().gpsSaveQueue.keySet()){
				DbManager.instance().dbs.batchSaveGpsSignal(company);
			}
			for (Company company : CacheManager.instance().alarmSaveQueue.keySet()) {
				DbManager.instance().dba.batchSaveAlarmInfo(company);
			}
			for (Company company : CacheManager.instance().hookArmCaseSignalSaveQueue.keySet()) {
				DbManager.instance().dbs.batchSaveGpsHookArmCaseSignal(company);
			}
			for (Company company : CacheManager.instance().hookArmCaseAlarmSaveQueue.keySet()) {
				DbManager.instance().dba.batchSaveHookArmAlarmInfo(company);
			}
			//保存垃圾称重数据
			for (Company company : CacheManager.instance().saveGarbageDataQueue.keySet()) {
				DbManager.instance().dg.batchGarbageData(company);
			}
			//保存垃圾称重报警数据
			for (Company company : CacheManager.instance().saveGarbageAlarmDataQueue.keySet()) {
				DbManager.instance().dg.batchGarbageAlarmData(company);
			}
			//保存油量变化
			for (Company company : CacheManager.instance().oilChangeBeanSaveQueue.keySet()) {
				DbManager.instance().dbo.insert(company);
			}
			//更新油卡中剩余油量
			for (Company company : CacheManager.instance().updateOilCardQueue.keySet()) {
				DbManager.instance().dbo.updateOilCard(company);
			}
			Logger.println("任务执行完毕，下次启动时间：" + context.getNextFireTime());
		} catch (Exception e) {
			Logger.err(e);
		}
	}

}
